package com.cq.hd.order.utils;

import com.alibaba.fastjson.JSON;
import com.cq.hd.common.enums.biz.ActiveEnrollInfoTypeEnum;
import com.cq.hd.order.api.dto.ActiveEnrollInfoOptionDto;
import com.cq.hd.order.api.dto.ActiveEnrollInfoSonDto;
import com.cq.hd.order.api.vo.OrderEnrollInfoVo;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单报名信息工具类
 *
 * @author Justin.Chen
 * @since 2024/09/18 10:09
 */
public class OrderEnrollInfoUtil {

    public static List<OrderEnrollInfoVo> parseEnrollInfos(String enrollInfoJson) {
        if (StringUtils.isBlank(enrollInfoJson)) {
            return Collections.emptyList();
        }
        // 报名信息
        List<OrderEnrollInfoVo> orderEnrollInfoVos = new ArrayList<>();

        List<ActiveEnrollInfoSonDto> activeEnrollInfoSonDtos = JSON.parseArray(enrollInfoJson, ActiveEnrollInfoSonDto.class);
        for (ActiveEnrollInfoSonDto activeEnrollInfoSonDto : activeEnrollInfoSonDtos) {
            String title = activeEnrollInfoSonDto.getTitle();
            Integer sort = activeEnrollInfoSonDto.getSort();

            OrderEnrollInfoVo orderEnrollInfoVo = new OrderEnrollInfoVo();
            orderEnrollInfoVo.setTitle(title);
            orderEnrollInfoVo.setSort(sort);

            // 类型(1-填写项，2-单选项，3-多选项)
            Integer type = activeEnrollInfoSonDto.getType();
            if (ActiveEnrollInfoTypeEnum.WRITE.getValue().equals(type)) {
                // 填写
                orderEnrollInfoVo.setValue(activeEnrollInfoSonDto.getContent());
            } else if (ActiveEnrollInfoTypeEnum.SINGLE.getValue().equals(type)) {
                // 单选
                StringBuilder valueStringBuilder = new StringBuilder();
                List<ActiveEnrollInfoOptionDto> activeEnrollInfoOptions = activeEnrollInfoSonDto.getActiveEnrollInfoOptions();
                for (ActiveEnrollInfoOptionDto activeEnrollInfoOption : activeEnrollInfoOptions) {
                    // 勾选状态：1-未勾选，2-已勾选
                    Integer checkState = activeEnrollInfoOption.getCheckState();
                    if (checkState != null && checkState == 2) {
                        String optionText = activeEnrollInfoOption.getOptionText();
                        valueStringBuilder.append(optionText).append("、");
                    }
                }

                if (valueStringBuilder.length() > 0) {
                    valueStringBuilder.deleteCharAt(valueStringBuilder.length() - 1);
                    String value = valueStringBuilder.toString();
                    orderEnrollInfoVo.setValue(value);
                }
            } else {
                // 多选
                StringBuilder valueStringBuilder = new StringBuilder();
                List<ActiveEnrollInfoOptionDto> activeEnrollInfoOptions = activeEnrollInfoSonDto.getActiveEnrollInfoOptions();
                for (ActiveEnrollInfoOptionDto activeEnrollInfoOption : activeEnrollInfoOptions) {
                    // 勾选状态：1-未勾选，2-已勾选
                    Integer checkState = activeEnrollInfoOption.getCheckState();
                    if (checkState != null && checkState == 2) {
                        String optionText = activeEnrollInfoOption.getOptionText();
                        valueStringBuilder.append(optionText).append("、");
                    }
                }

                if (valueStringBuilder.length() > 0) {
                    valueStringBuilder.deleteCharAt(valueStringBuilder.length() - 1);
                    String value = valueStringBuilder.toString();
                    orderEnrollInfoVo.setValue(value);
                }
            }
            orderEnrollInfoVos.add(orderEnrollInfoVo);
        }
        // 根据排序值正序
        return orderEnrollInfoVos.stream().sorted(Comparator.comparing(OrderEnrollInfoVo::getSort)).collect(Collectors.toList());
    }

}
